<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>Electron+Vue项目搭建 | LenlBlog</title><meta name="author" content="Lenl,lenl412@163.com"><meta name="copyright" content="Lenl"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="ffffff"><meta name="description" content="记录一下Electron结合Vue搭建项目的记录">
<meta property="og:type" content="article">
<meta property="og:title" content="Electron+Vue项目搭建">
<meta property="og:url" content="https://lienle.gitee.io/notes/electronVue/index.html">
<meta property="og:site_name" content="LenlBlog">
<meta property="og:description" content="记录一下Electron结合Vue搭建项目的记录">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png">
<meta property="article:published_time" content="2023-05-14T02:36:59.000Z">
<meta property="article:modified_time" content="2023-06-09T15:51:30.894Z">
<meta property="article:author" content="Lenl">
<meta property="article:tag" content="Vue">
<meta property="article:tag" content="Electron">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png"><link rel="shortcut icon" href="/img/lenl.png"><link rel="canonical" href="https://lienle.gitee.io/notes/electronVue/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta name="baidu-site-verification" content="codeva-6HME93uF43"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?629608ec0c773bd9f96cb72389693ab9";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: Lenl","link":"链接: ","source":"来源: LenlBlog","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: true,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Electron+Vue项目搭建',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-06-09 23:51:30'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', 'ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script></head><body><!--include ./fullpage-loading.pug--><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="wizard-scene"><div class="wizard-objects"><div class="wizard-square"></div><div class="wizard-circle"></div><div class="wizard-triangle"></div></div><div class="wizard"><div class="wizard-body"></div><div class="wizard-right-arm"><div class="wizard-right-hand"></div></div><div class="wizard-left-arm"><div class="wizard-left-hand"></div></div><div class="wizard-head"><div class="wizard-beard"></div><div class="wizard-face"><div class="wizard-adds"></div></div><div class="wizard-hat"><div class="wizard-hat-of-the-hat"></div><div class="wizard-four-point-star --first"></div><div class="wizard-four-point-star --second"></div><div class="wizard-four-point-star --third"></div></div></div></div></div></div><script>const preloader = {
  endLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = 'hidden';
    document.getElementById('loading-box').classList.remove("loaded")
  }
}

preloader.initLoading()
window.addEventListener('load',()=> { preloader.endLoading() })
//避免加载时间过长
setTimeout(function(){preloader.endLoading();}, 3000);

if (false) {
  document.addEventListener('pjax:send', () => { preloader.initLoading() })
  document.addEventListener('pjax:complete', () => { preloader.endLoading() })
}</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "/img/hexo_loading.gif" data-lazy-src="/img/lenl.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">17</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">25</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 存档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 分享</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/collect/"><i class="fa-fw fas fa-book"></i><span> 大佬们の干货</span></a></li><li><a class="site-page child" href="/resources/"><i class="fa-fw fab fa-dropbox"></i><span> 屯屯鼠の资源</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg fixed" id="page-header" style="background-image: url('https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png')"><nav id="nav"><span id="blog-info"><a href="/" title="LenlBlog"><img class="site-icon" src= "/img/hexo_loading.gif" data-lazy-src="/img/lenl.png"/><span class="site-name">LenlBlog</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 存档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 分享</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/collect/"><i class="fa-fw fas fa-book"></i><span> 大佬们の干货</span></a></li><li><a class="site-page child" href="/resources/"><i class="fa-fw fab fa-dropbox"></i><span> 屯屯鼠の资源</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Electron+Vue项目搭建</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-05-14T02:36:59.000Z" title="发表于 2023-05-14 10:36:59">2023-05-14</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-06-09T15:51:30.894Z" title="更新于 2023-06-09 23:51:30">2023-06-09</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E7%BC%96%E7%A8%8B%E9%9A%8F%E8%AE%B0/">编程随记</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.5k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>5分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Electron+Vue项目搭建"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span><span class="post-meta-separator">|</span><span class="post-meta-commentcount"><i class="far fa-comments fa-fw post-meta-icon"></i><span class="post-meta-label">评论数:</span><a href="/notes/electronVue/#post-comment" itemprop="discussionUrl"><span class="valine-comment-count" data-xid="/notes/electronVue/" itemprop="commentCount"><i class="fa-solid fa-spinner fa-spin"></i></span></a></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><div class="note info simple"><p>最近需要开发一个桌面程序，但是由于早年使用Winform，那个UI设计让我及其头疼，所以还是想使用html+js+css这一套来搭建界面。刚好，有这个想法的远不止我一个，electron就是为此而生。</p>
<p>学了一阵子electron后，觉得搭一个electron+vue的项目试一试，在这里记录一下经验。</p>
</div>
<div class="note warning simple"><p>补充：</p>
<p>根据学习教程 <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1xd4y1J7dB">Electron入门</a> 做了一些尝试，教程中基于Electron与React做了一个markdown编辑器的小项目。本人根据自己掌握的知识，用Electron与Vue进行了复现。以下链接可供交流学习参考：</p>
<p><a target="_blank" rel="noopener" href="https://gitee.com/lienle/electron_markdown_editor_demo">electron_vue_markdown_editor源码仓库</a></p>
<p><a target="_blank" rel="noopener" href="https://gitee.com/lienle/electron_markdown_editor_demo/releases/tag/v1">electron_vue_markdown_editor 初版安装包下载 </a></p>
</div> 
<h3 id="安装依赖">安装依赖</h3>
<p>首先设置淘宝镜像，加速安装：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">npm install --registry=https://registry.npm.taobao.org</span><br></pre></td></tr></table></figure>
<p>如果没有vue脚手架需要安装</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">npm install -g @vue/cli</span><br></pre></td></tr></table></figure>
<p>我们首先来创建一个vue项目：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">vue create <span class="built_in">test</span></span><br></pre></td></tr></table></figure>
<p>选项中选择Default即可。</p>
<p>安装完成后，进入目录，然后安装electron构建插件</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line"><span class="built_in">cd</span> <span class="built_in">test</span></span><br><span class="line">vue add electron-builder</span><br></pre></td></tr></table></figure>
<p>之后会让选择版本，选择最新的即可，等待其安装完成。</p>
<p>启动项目：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">npm run electron:serve</span><br></pre></td></tr></table></figure>
<p>即可看到启动后的electron桌面程序。</p>
<p>这里可能会出现一个问题：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">Failed to fetch extension, trying 4 more <span class="built_in">times</span></span><br></pre></td></tr></table></figure>
<p>这是项目启动后安装vue插件导致的，可以在background.js注释掉以下代码即可：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">app.<span class="title function_">on</span>(<span class="string">&#x27;ready&#x27;</span>, <span class="keyword">async</span> () =&gt; &#123;</span><br><span class="line">  <span class="comment">// 安装插件会导致 Failed to Fetch extension错误</span></span><br><span class="line">  <span class="comment">// if (isDevelopment &amp;&amp; !process.env.IS_TEST) &#123;</span></span><br><span class="line">  <span class="comment">//   // Install Vue Devtools</span></span><br><span class="line">  <span class="comment">//   try &#123;</span></span><br><span class="line">  <span class="comment">//     await installExtension(VUEJS_DEVTOOLS)</span></span><br><span class="line">  <span class="comment">//   &#125; catch (e) &#123;</span></span><br><span class="line">  <span class="comment">//     console.error(&#x27;Vue Devtools failed to install:&#x27;, e.toString())</span></span><br><span class="line">  <span class="comment">//   &#125;</span></span><br><span class="line">  <span class="comment">// &#125;</span></span><br><span class="line">  <span class="comment">//注意不要把这句注释掉了</span></span><br><span class="line">  <span class="title function_">createWindow</span>()</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="项目结构改造">项目结构改造</h3>
<p>安装完 <code>electron-builder</code> 后可以看到<code>src</code>目录下多了<code>background.js</code> 文件，<code>package.json</code> 中多了一些启动脚本命令。</p>
<p>我们知道，electron是分为主进程和渲染进程的，显然，vue的页面部分是属于渲染进程的，而安装插件后多出来的<code>background.js</code>属于主进程，因此我们可以将文件结构做一些改造。</p>
<ul class="lvl-0">
<li class="lvl-2">
<p>在 <code>src</code>目录下新建两个文件夹：<code>src/main</code> 和 <code>src/renderer</code></p>
</li>
<li class="lvl-2">
<p>将 <code>background.js</code> 移动到 <code>src/main</code> 下，并重命名为 <code>index.js</code></p>
</li>
<li class="lvl-2">
<p>将 <code>src</code> 目录下原来的其他文件移动到 <code>src/renderer</code> 下</p>
</li>
</ul>
<p>这样以后， <code>src</code> 目录的结构就会如下：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">├─src                          <span class="comment"># 源码目录</span></span><br><span class="line">│  ├─main                      <span class="comment"># 主进程目录</span></span><br><span class="line">│  │  └─index.js               <span class="comment"># 主进程入口</span></span><br><span class="line">│  └─renderer                  <span class="comment"># 渲染进程文件夹</span></span><br><span class="line">│      ├─assets</span><br><span class="line">│      ├─components</span><br><span class="line">│      ├─App.vue</span><br><span class="line">│      └─main.js               <span class="comment"># 渲染进程入口</span></span><br></pre></td></tr></table></figure>
<p>但是这样一来入口文件的位置就被我们改变了，所以需要添加一个 <code>vue.config.js</code> 修改入口文件：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="variable language_">module</span>.<span class="property">exports</span> = &#123;</span><br><span class="line">  <span class="attr">pluginOptions</span>: &#123;</span><br><span class="line">    <span class="attr">electronBuilder</span>: &#123;</span><br><span class="line">      <span class="attr">mainProcessFile</span>: <span class="string">&#x27;src/main/index.js&#x27;</span>, <span class="comment">// 主进程入口文件</span></span><br><span class="line">      <span class="attr">rendererProcessFile</span>: <span class="string">&#x27;src/renderer/main.js&#x27;</span>, <span class="comment">// 渲染进程入口文件</span></span><br><span class="line">      <span class="attr">mainProcessWatch</span>: [<span class="string">&#x27;src/main&#x27;</span>], <span class="comment">// 检测主进程文件在更改时将重新编译主进程并重新启动</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>然后重新运行 <code>npm run electron:serve</code> 即可。</p>
<p>这样一个项目的壳子就创建好了，我们可以在renderer下按照之前开发vue的方式继续进行开发桌面应用了。</p>
<h3 id="项目的打包">项目的打包</h3>
<p>上面的步骤完成后，即可像做web开发一样开始进行客户端的开发了。但是build后，我们希望用户能够自主选择安装位置，因此需要使用nsis这个win桌面程序打包工具。</p>
<p>首先需要再电脑上安装nsis软件，可自行下载安装；</p>
<p>然后再项目根目录(与vue.config.js同级)新建一个脚本installer.nsh如下：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">!macro preInit</span><br><span class="line">  SetRegView 64</span><br><span class="line">  WriteRegExpandStr HKLM &quot;$&#123;INSTALL_REGISTRY_KEY&#125;&quot; InstallLocation &quot;D:\Program Files\$&#123;PRODUCT_NAME&#125;&quot;</span><br><span class="line">  WriteRegExpandStr HKCU &quot;$&#123;INSTALL_REGISTRY_KEY&#125;&quot; InstallLocation &quot;D:\Program Files\$&#123;PRODUCT_NAME&#125;&quot;</span><br><span class="line">  SetRegView 32</span><br><span class="line">  WriteRegExpandStr HKLM &quot;$&#123;INSTALL_REGISTRY_KEY&#125;&quot; InstallLocation &quot;D:\Program Files (x86)\$&#123;PRODUCT_NAME&#125;&quot;</span><br><span class="line">  WriteRegExpandStr HKCU &quot;$&#123;INSTALL_REGISTRY_KEY&#125;&quot; InstallLocation &quot;D:\Program Files (x86)\$&#123;PRODUCT_NAME&#125;&quot;</span><br><span class="line">!macroend</span><br></pre></td></tr></table></figure>
<p>再完善vue.config.js中的打包相关内容，如下：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="variable language_">module</span>.<span class="property">exports</span> = &#123;</span><br><span class="line">    <span class="attr">pluginOptions</span>: &#123;</span><br><span class="line">        <span class="attr">electronBuilder</span>: &#123;</span><br><span class="line">            <span class="attr">mainProcessFile</span>: <span class="string">&#x27;src/main/index.js&#x27;</span>, <span class="comment">// 主进程入口文件</span></span><br><span class="line">            <span class="attr">rendererProcessFile</span>: <span class="string">&#x27;src/renderer/main.js&#x27;</span>, <span class="comment">// 渲染进程入口文件</span></span><br><span class="line">            <span class="attr">mainProcessWatch</span>: [<span class="string">&#x27;src/main&#x27;</span>], <span class="comment">// 检测主进程文件在更改时将重新编译主进程并重新启动</span></span><br><span class="line">            <span class="attr">nodeIntegration</span>: <span class="literal">true</span>,</span><br><span class="line">            <span class="comment">// 打包配置</span></span><br><span class="line">            <span class="attr">builderOptions</span>: &#123;</span><br><span class="line">                <span class="comment">// 发布者名称</span></span><br><span class="line">                <span class="attr">productName</span>: <span class="string">&#x27;lenl&#x27;</span>,</span><br><span class="line">                <span class="comment">// 包名</span></span><br><span class="line">                <span class="attr">appId</span>: <span class="string">&#x27;cn.lenl.text&#x27;</span>,</span><br><span class="line">                <span class="attr">copyright</span>: <span class="string">&#x27;lenl copyright&#x27;</span>,</span><br><span class="line">                <span class="comment">// 更新的服务器</span></span><br><span class="line">                <span class="comment">// publish: [</span></span><br><span class="line">                <span class="comment">//     &#123;</span></span><br><span class="line">                <span class="comment">//         provider: &#x27;generic&#x27;,</span></span><br><span class="line">                <span class="comment">//         url: &#x27;http://127.0.0.1/app/&#x27;</span></span><br><span class="line">                <span class="comment">//     &#125;</span></span><br><span class="line">                <span class="comment">// ],</span></span><br><span class="line">                <span class="attr">asar</span>: <span class="literal">false</span>,</span><br><span class="line">                <span class="comment">// 不需要打包至asar中的文件如数据库文件,icons文件</span></span><br><span class="line">                <span class="comment">// extraResources: [</span></span><br><span class="line">                <span class="comment">//     &#123;</span></span><br><span class="line">                <span class="comment">//         from: &#x27;./data/db.db&#x27;,</span></span><br><span class="line">                <span class="comment">//         to: &#x27;../data/db.db&#x27;,</span></span><br><span class="line">                <span class="comment">//         filter: [&#x27;**/*&#x27;]</span></span><br><span class="line">                <span class="comment">//     &#125;, &#x27;./icons&#x27;],</span></span><br><span class="line">                <span class="attr">win</span>: &#123;</span><br><span class="line">                    <span class="comment">// 图标文件大小为 256*256</span></span><br><span class="line">                    <span class="attr">icon</span>: <span class="string">&#x27;./public/logo.ico&#x27;</span>,</span><br><span class="line">                    <span class="attr">target</span>: [</span><br><span class="line">                        &#123;</span><br><span class="line">                            <span class="attr">target</span>: <span class="string">&#x27;nsis&#x27;</span>,</span><br><span class="line">                            <span class="attr">arch</span>: [</span><br><span class="line">                                <span class="comment">// &#x27;x64&#x27;,</span></span><br><span class="line">                                <span class="string">&#x27;ia32&#x27;</span></span><br><span class="line">                            ]</span><br><span class="line">                        &#125;</span><br><span class="line">                    ],</span><br><span class="line">                    <span class="comment">// 打包权限 asInvoker | highestAvailable</span></span><br><span class="line">                    <span class="attr">requestedExecutionLevel</span>: <span class="string">&#x27;highestAvailable&#x27;</span></span><br><span class="line">                &#125;,</span><br><span class="line">                <span class="comment">// 安装包名称，可自行配置</span></span><br><span class="line">                <span class="attr">artifactName</span>: <span class="string">&#x27;$&#123;productName&#125;_Setup_$&#123;version&#125;_$&#123;platform&#125;.$&#123;ext&#125;&#x27;</span>,</span><br><span class="line">                <span class="attr">nsis</span>: &#123;</span><br><span class="line">                    <span class="comment">// 一键安装，如果设为true，nsis设置就无意义请直接删除 nsis 配置</span></span><br><span class="line">                    <span class="attr">oneClick</span>: <span class="literal">false</span>,</span><br><span class="line">                    <span class="comment">// true全用户安装【目录为：C:\Program Files (x86)】，false安装到当前用户</span></span><br><span class="line">                    <span class="attr">perMachine</span>: <span class="literal">true</span>,</span><br><span class="line">                    <span class="comment">// 允许请求提升。 如果为false，则用户必须使用提升的权限重新启动安装程序。</span></span><br><span class="line">                    <span class="attr">allowElevation</span>: <span class="literal">true</span>,</span><br><span class="line">                    <span class="comment">// 允许修改安装目录</span></span><br><span class="line">                    <span class="attr">allowToChangeInstallationDirectory</span>: <span class="literal">true</span>,</span><br><span class="line">                    <span class="comment">// 创建桌面图标</span></span><br><span class="line">                    <span class="attr">createDesktopShortcut</span>: <span class="literal">true</span>,</span><br><span class="line">                    <span class="comment">// 创建开始菜单图标</span></span><br><span class="line">                    <span class="attr">createStartMenuShortcut</span>: <span class="literal">true</span>,</span><br><span class="line">                    <span class="comment">// 快捷方式的名称,默认为应用程序名称</span></span><br><span class="line">                    <span class="comment">// shortcutName: &#x27;HX&#x27;,</span></span><br><span class="line">                    <span class="comment">// 安装图标</span></span><br><span class="line">                    <span class="attr">installerIcon</span>: <span class="string">&#x27;./public/logo.ico&#x27;</span>,</span><br><span class="line">                    <span class="comment">// 卸载图标</span></span><br><span class="line">                    <span class="attr">uninstallerIcon</span>: <span class="string">&#x27;./public/logo.ico&#x27;</span>,</span><br><span class="line">                    <span class="comment">// 安装时头部图标</span></span><br><span class="line">                    <span class="attr">installerHeaderIcon</span>: <span class="string">&#x27;./public/logo.ico&#x27;</span>,</span><br><span class="line">                    <span class="comment">// 配置 nsn 如修改默认安装目录</span></span><br><span class="line">                    <span class="attr">include</span>: <span class="string">&#x27;./installer.nsh&#x27;</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>之后运行 <code>npm run electron:build</code> 生成的exe文件就是可以选择安装路径的了。</p>
<p>这个过程中可能会报一些错，需要下载一些东西，详情参考这篇文章：<a target="_blank" rel="noopener" href="https://blog.csdn.net/Gabriel_wei/article/details/92794291">nsis打包electron可能遇到的问题</a></p>
<p>需要注意的是，项目路径中不能含有中文，否则打包会失败。</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://lienle.gitee.io">Lenl</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://lienle.gitee.io/notes/electronVue/">https://lienle.gitee.io/notes/electronVue/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://lienle.gitee.io" target="_blank">LenlBlog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Vue/">Vue</a><a class="post-meta__tags" href="/tags/Electron/">Electron</a></div><div class="post_share"><div class="social-share" data-image="https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/wechat.png" target="_blank"><img class="post-qr-code-img" src= "/img/hexo_loading.gif" data-lazy-src="/img/wechat.png" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/alipay.png" target="_blank"><img class="post-qr-code-img" src= "/img/hexo_loading.gif" data-lazy-src="/img/alipay.png" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/notes/chunkFile/" title="基于SpringBoot与Vue的大文件分片上传与流式下载"><img class="cover" src= "/img/hexo_loading.gif" data-lazy-src="https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">基于SpringBoot与Vue的大文件分片上传与流式下载</div></div></a></div><div class="next-post pull-right"><a href="/notes/PdfView/" title="基于pdf.js的前后端分离式PDF文件预览实现"><img class="cover" src= "/img/hexo_loading.gif" data-lazy-src="https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">基于pdf.js的前后端分离式PDF文件预览实现</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/notes/electronLearn/" title="Electron学习入门笔记"><img class="cover" src= "/img/hexo_loading.gif" data-lazy-src="https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-06</div><div class="title">Electron学习入门笔记</div></div></a></div><div><a href="/notes/PdfView/" title="基于pdf.js的前后端分离式PDF文件预览实现"><img class="cover" src= "/img/hexo_loading.gif" data-lazy-src="https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-08-06</div><div class="title">基于pdf.js的前后端分离式PDF文件预览实现</div></div></a></div><div><a href="/notes/chunkFile/" title="基于SpringBoot与Vue的大文件分片上传与流式下载"><img class="cover" src= "/img/hexo_loading.gif" data-lazy-src="https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-13</div><div class="title">基于SpringBoot与Vue的大文件分片上传与流式下载</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "/img/hexo_loading.gif" data-lazy-src="/img/lenl.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Lenl</div><div class="author-info__description">心有所向，日复一日，必有精进~</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">17</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">25</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/vimianma"><i class="fab fa-github"></i><span>View me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitee.com/lienle" target="_blank" title="Gitee"><i class="fa-brands fa-git" style="color: #c71d23;"></i></a><a class="social-icon" href="https://github.com/vimianma" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="mailto:lenl412@163.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">本站意在记录个人生活中的所学所感所想所获；<br/>心有所向，日复一日，必有精进~~~<br>请多多指教~</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%85%E4%BE%9D%E8%B5%96"><span class="toc-number">1.</span> <span class="toc-text">安装依赖</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%A1%B9%E7%9B%AE%E7%BB%93%E6%9E%84%E6%94%B9%E9%80%A0"><span class="toc-number">2.</span> <span class="toc-text">项目结构改造</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%A1%B9%E7%9B%AE%E7%9A%84%E6%89%93%E5%8C%85"><span class="toc-number">3.</span> <span class="toc-text">项目的打包</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/notes/dockerNotes/" title="Docker备忘笔记"><img src= "/img/hexo_loading.gif" data-lazy-src="https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Docker备忘笔记"/></a><div class="content"><a class="title" href="/notes/dockerNotes/" title="Docker备忘笔记">Docker备忘笔记</a><time datetime="2024-04-06T13:17:09.000Z" title="发表于 2024-04-06 21:17:09">2024-04-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/notes/mysqlInstall/" title="mysql压缩包方式本地安装"><img src= "/img/hexo_loading.gif" data-lazy-src="https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="mysql压缩包方式本地安装"/></a><div class="content"><a class="title" href="/notes/mysqlInstall/" title="mysql压缩包方式本地安装">mysql压缩包方式本地安装</a><time datetime="2024-01-03T05:33:33.000Z" title="发表于 2024-01-03 13:33:33">2024-01-03</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/notes/PdfView/" title="基于pdf.js的前后端分离式PDF文件预览实现"><img src= "/img/hexo_loading.gif" data-lazy-src="https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="基于pdf.js的前后端分离式PDF文件预览实现"/></a><div class="content"><a class="title" href="/notes/PdfView/" title="基于pdf.js的前后端分离式PDF文件预览实现">基于pdf.js的前后端分离式PDF文件预览实现</a><time datetime="2023-08-06T02:18:26.000Z" title="发表于 2023-08-06 10:18:26">2023-08-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/notes/electronVue/" title="Electron+Vue项目搭建"><img src= "/img/hexo_loading.gif" data-lazy-src="https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Electron+Vue项目搭建"/></a><div class="content"><a class="title" href="/notes/electronVue/" title="Electron+Vue项目搭建">Electron+Vue项目搭建</a><time datetime="2023-05-14T02:36:59.000Z" title="发表于 2023-05-14 10:36:59">2023-05-14</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/notes/chunkFile/" title="基于SpringBoot与Vue的大文件分片上传与流式下载"><img src= "/img/hexo_loading.gif" data-lazy-src="https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="基于SpringBoot与Vue的大文件分片上传与流式下载"/></a><div class="content"><a class="title" href="/notes/chunkFile/" title="基于SpringBoot与Vue的大文件分片上传与流式下载">基于SpringBoot与Vue的大文件分片上传与流式下载</a><time datetime="2023-05-13T13:57:28.000Z" title="发表于 2023-05-13 21:57:28">2023-05-13</time></div></div></div></div></div></div></main><footer id="footer" style="background: -#484847"><div id="footer-wrap"><div class="copyright">&copy;2023 - 2024 By Lenl</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">Hi, welcome to my blog !</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: 'dBlOeCLNhBxbSdZO44fRKdBa-gzGzoHsz',
      appKey: 'DCSQoDhPl0UOF7Ix9Imdfd9X',
      avatar: 'monsterid',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !false) {
  if (false) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script></div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div class="no-result" id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div></body></html>